package main

func min(arr ...int) int {
	m := 1<<31 - 1
	for _, v := range arr {
		if v < m {
			m = v
		}
	}
	return m
}

func maximalSquare(matrix [][]byte) int {
	if len(matrix) == 0 {
		return 0
	}
	bp := make([][]int, len(matrix))
	max := 0
	for i, row := range matrix {
		bp[i] = make([]int, len(matrix[0]))
		for j, v := range row {
			if i == 0 {
				if v == '1' {
					bp[i][j] = 1
				} // else bp[i][j] = 0
			} else if j == 0 {
				if v == '1' {
					bp[i][j] = 1
				}
			} else if matrix[i][j] == '1' {
				bp[i][j] = min(bp[i-1][j], bp[i][j-1], bp[i-1][j-1]) + 1
			}

			if bp[i][j] > max {
				max = bp[i][j]
			}
		}
	}
	return max * max
}

func main() {
	//s := [][]byte{
	//	{'1', '0', '1', '0', '0'},
	//	{'1', '0', '1', '1', '1'},
	//	{'1', '1', '1', '1', '1'},
	//	{'1', '0', '0', '1', '0'},
	//}
	s := [][]byte{
		{'0', '0', '0', '1'},
		{'1', '1', '0', '1'},
		{'1', '1', '1', '1'},
		{'0', '1', '1', '1'},
		{'0', '1', '1', '1'},
	}
	println(maximalSquare(s))
}
